﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stack>
#include<list>
using namespace std;

struct TreeNode
{
	char _c;
	TreeNode* _left;
	TreeNode* _right;
	TreeNode(char c) 
		: _c(c)
		, _left(nullptr)
		, _right(nullptr) 
	{}
};

TreeNode* expTree(string& s,stack<TreeNode*>& num)
{
	for (auto e : s)
	{
		if (e >= 'a' && e <= 'z')
		{
			TreeNode* root = new TreeNode(e);
			num.push(root);
		}
		else
		{
			TreeNode* root = new TreeNode(e);
			root->_right = num.top();
			num.pop();
			root->_left = num.top();
			num.pop();
			num.push(root);
		}
	}
	return num.top();
}

void rightexp(TreeNode* cur, list<char>& lt, int level)
{
	if (cur == nullptr)
	{
		return;
	}
	if (level == lt.size())
	{
		lt.push_back(cur->_c);
	}
	rightexp(cur->_right, lt, level + 1);
	rightexp(cur->_left, lt, level + 1);
}



//小写字母表示操作数，大写字母表示运算符
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string s;
		cin >> s;
		stack<TreeNode*> num;
		TreeNode* root = expTree(s, num);
		list<char> lt;
		rightexp(root, lt, 0);
		for (auto e : lt)
		{
			cout << e;
		}
		cout << endl;
}




